module one {
	export class SecondUtils {
		public constructor() {
			this.seconds = {};
		}

		private intervalID: number;
		start(): void {
			this.intervalID = one.setInterval(this.onHandler.bind(this), 100);
		}

		private onHandler(): void {
			let time = Date.now();

			for (let id in this.seconds) {
				let info = this.seconds[id];
				if (info.endTimeStamp <= time) {
					delete this.seconds[id];
					info.call(0);
				}
				else {
					info.call(Math.ceil((info.endTimeStamp - time) / 1000));
				}
			}
		}

		stop(): void {
			one.clearInterval(this.intervalID);
		}

		seconds: { [id: number]: { second: number, endTimeStamp: number, call: (leftSecond: number) => void } };

		static count: number = 1;
		addSecond(second: number, call: (leftSecond: number) => void): number {
			SecondUtils.count++;
			this.seconds[SecondUtils.count] = { second: second, endTimeStamp: Date.now() + second * 1000, call: call };

			return SecondUtils.count;
		}

		removeSecond(id: number): boolean {
			delete this.seconds[id];
			return true;
		}
	}

	export let secondUtils: SecondUtils = new one.SecondUtils();

	export function formatSecond(second: number, format: string): string {
		let result: string = format;
		second = Math.floor(second);

		if (format.indexOf("h") >= 0) {
			let h: number = Math.floor(second / 3600);
			second -= h * 3600;

			if (format.indexOf("hh") >= 0) {
				result = result.replace("hh", (h < 10 ? "0" + h : h + ""));
			}
			else {
				result = result.replace("h", h + "");
			}
		}

		if (format.indexOf("m") >= 0) {
			let m: number = Math.floor(second / 60);
			second -= m * 60;

			if (format.indexOf("mm") >= 0) {
				result = result.replace("mm", (m < 10 ? '0' + m : m + ""));
			}
			else {
				result = result.replace("m", m + "");
			}
		}

		if (format.indexOf("ss") >= 0) {
			result = result.replace("ss", (second < 10 ? '0' + second : second + ""));
		}
		else {
			result = result.replace("s", second + "");
		}

		return result;
	}

	export function formatTime(timeStamp: number, format: string): string {
		let date = new Date();
		date.setTime(timeStamp);

		let result: string = format;
		if (result.match("YYYY")) {
			result = result.replace('YYYY', date.getFullYear().toString());
		}
		else if (result.match("YY")) {
			result = result.replace('YY', date.getFullYear().toString().substr(2));
		}
		
		result = result.replace('MM', date.getMonth() < 9 ? "0" + (date.getMonth() + 1) + "" : (date.getMonth() + 1) + "");
		result = result.replace('DD', date.getDate() < 10 ? "0" + (date.getDate()) + "" : (date.getDate()) + "");

		let h = date.getHours();
		let m = date.getMinutes();
		let second = date.getSeconds();
		result = result.replace("hh", (h < 10 ? "0" + h : h + ""));
		result = result.replace("mm", (m < 10 ? '0' + m : m + ""));
		result = result.replace("ss", (second < 10 ? '0' + second : second + ""));
		return result;
	}

	export function formatLastTime(timeStamp: number, format: string): string {
		let nowT = Date.now();
		let deltaT = Math.round((nowT - timeStamp) / 1000);

		if (deltaT < 60) {
			return deltaT + "秒前"
		}
		if (deltaT < 60 * 60) {
			return Math.round(deltaT / 60) + "分前"
		}
		if (deltaT < 24 * 60 * 60) {
			return Math.round(deltaT / 60 / 60) + "小时前"
		}

		let date = new Date();
		date.setTime(timeStamp);
		return this.formatTime(timeStamp, format);
	}

	export function formatLastTime2(timeStamp: number, format: string): string {
		let nowT = Date.now();
		let deltaT = Math.round((nowT - timeStamp) / 1000);

		if (deltaT < 60) {
			return deltaT + "秒前"
		}
		if (deltaT < 60 * 60) {
			return Math.round(deltaT / 60) + "分前"
		}
		if (deltaT < 24 * 60 * 60) {
			return Math.round(deltaT / 60 / 60) + "小时前"
		}

		return Math.round(deltaT / 60 / 60 / 24) + "天前"
	}

	export function toFixed(num: number, digit: number): string {
		let numStr = Math.round(num).toString();

		while (numStr.length < digit) {
			numStr = "0" + numStr;
		}

		return numStr;
	}
}